<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - nimotsu</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-nimotsu.html">here</a>.</p>

<div id="sagawa" style="display:none;">
    <b>${query}</b> に該当するSagawaの荷物一覧:
    <div style="clear:both;">
        <br />
        ${detail}
    </div>
</div>

<div id="kuronekoyamato" style="display:none;">
    <b>${query}</b> に該当するヤマト運輸の荷物一覧:
    <div style="clear:both;">
        <br />
        ${detail}
    </div>
</div>

<div id="yuupack" style="display:none;">
    <b>${query}</b> に該当するゆうパックの荷物:
    <div style="clear:both;">
        <br />
        ${detail}
    </div>
</div>

<div id="kakitome" style="display:none;">
    <b>${query}</b> に該当する書留の記録:
    <div style="clear:both;">
        <br />
        ${detail}
    </div>
</div>
<script class="commands"><![CDATA[

CmdUtils.CreateCommand({
    name: "sagawa",
    description: "佐川急便荷物問い合わせ",
    takes: {search: noun_arb_text},
    icon: "http://www.sagawa-exp.co.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 8) {
            var previewStr = "佐川急便荷物問い合わせ";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> の荷物を問い合わせます...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: getSagawaUrl(searchText),
          dataType: "html",
          
          error: function() {
            previewBlock.innerHTML = "Error asking Sagawa.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            
            var detail = "";
            /*
            responseData.find(".ichiran-fg + .ichiran-fg-msrc2-2").each(function(){ 
                detail = detail + jQuery(this).html() + "<br />" 
            } );
            */
            
            detail += jQuery("<p>").append(responseData.find("table.ichiran-bg").parent().clone()).html();
            
            detail = jQuery(detail);
            
            jQuery("table.ichiran-bg",detail).each(function(){
                
                jQuery(".ichiran-bg",this).css({  margin:"2px 2px 6px 2px",
                                    border: "1px solid #999999",
                                    "-moz-border-radius":"2px" });
                
                Utils.reportInfo(jQuery("<p>").append(jQuery(this).clone()).html());
            });
            
            var previewData = {
              query: searchText,
              detail: jQuery("<p>").append(jQuery(detail).clone()).html()
            };
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
              jQuery("#sagawa", feed.dom).html(),
              previewData);
          }
        });

    },

    execute: function( directObject ) {
            Utils.openUrlInBrowser( getSagawaUrl(directObject.text) );
    }
});

function getSagawaUrl(searchText){
    
    var url = "http://k2k.sagawa-exp.co.jp/cgi-bin/mall.mmcgi?{param}";
    var i = 0;
    var req = "";
    var items = searchText.split(" ",10);
    
    for(num in items ){
        //Utils.reportInfo(items[num]);
        var oku = "oku" + parseInt(++i).fillZero(2) + "=" + items[num] + "&";
        req = req + oku;
    }
    //Utils.reportInfo(req);
    url = url.replace(/%s|{param}/g, req );
    return url;

}

CmdUtils.CreateCommand({
    name: "kuronekoyamato",
    description: "ヤマト運輸荷物問い合わせ",
    takes: {search: noun_arb_text},
    icon: "http://www.kuronekoyamato.co.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 8) {
            var previewStr = "ヤマト運輸荷物問い合わせ";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> の荷物を問い合わせます...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        
        CmdUtils.previewAjax(previewBlock, {
          type: "POST",
          url: "http://toi.kuronekoyamato.co.jp/cgi-bin/tneko",
          data: getKuronekoData(searchText),
          dataType: "html",
          
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            previewBlock.innerHTML = "Error asking Kuronekoyamato.";
          },
          
          success: function(responseData, dataType) {
            
            //responseData = responseData.replace(/(<head>)/ig,'$1<base href="http://toi.kuronekoyamato.co.jp/cgi-bin/tneko">');
            
            responseData = jQuery(responseData);
            
            var detail = '<table style="font-size:85%;" id="detail">';
            responseData.find(".tb_c").each(function(){ 
                var jt = jQuery(this);
                if(jt.find("TD").length < 2){
                    jt.find("TD").attr("colspan",7 - jt.find("TD").length);
                    jt.find("TD").prepend( jQuery.trim( jt.prev().prev().prev().prev().text() )
                                          + "&nbsp;<B>"
                                          + jQuery.trim( jt.prev().prev().prev().text() ) 
                                          + "&nbsp;"
                                          + jQuery.trim( jt.prev().prev().text() )
                                          + "</B>&nbsp;" );
                    jt.find("TD").css({"background":"#666666"});
                }
                detail += jQuery(this).html();
            } );
            detail += "</table>";
            
            detail = jQuery(detail);
            
            detail.find("a").css({color:"white"});
            detail.find("table,td,th").css({"-moz-border-radius":"1px"});
            detail.find("th").attr("bgcolor", "#886633");
            
            detail = detail.outerHTML();
            
            var previewData = {
              query: searchText,
              detail: detail
            };
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
              jQuery("#kuronekoyamato", feed.dom).html(),
              previewData);
          }
        });

    },

    execute: function( directObject ) {
            Utils.openUrlInBrowser( "http://toi.kuronekoyamato.co.jp/cgi-bin/tneko", getKuronekoData(directObject.text) );
    }
});

function getKuronekoData(searchText){
    
    var i = 0;
    var req = "";
    var items = searchText.split(" ",10);
    var params = "number00=1&"
    for(num in items ){
        //Utils.reportInfo(items[num]);
        params = params + "number" + parseInt(++i).fillZero(2) + "=" + items[num] + "&";
    }
    //Utils.reportInfo(req);
    return params;

}


CmdUtils.CreateCommand({
    name: "yuupack",
    description: "ゆうパック荷物問い合わせ",
    takes: {search: noun_arb_text},
    icon: "http://www.japanpost.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 11) {
            var previewStr = "ゆうパック、EXPACK、ポスパケット荷物問い合わせ";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> の荷物を問い合わせます...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: 'http://tracking.post.japanpost.jp/service/singleSearch.do?searchKind=S002&locale=ja&reqCodeNo1=' + searchText,
          dataType: "html",
          
          error: function() {
            previewBlock.innerHTML = "Error asking Yuupack.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            
            responseData.find("A").each(function()jQuery(this).replaceWith("<div>" + jQuery(this).text() + "</div>"));
            
            var detail = "";
            
            detail = responseData.find(".message_font").text();
            detail = detail + responseData.find(".msg_warning").text();
            
            detail = detail + '<style>th,td{width:auto;height:auto;border:1px solid darkslategray;}</style><table style="width:80%;font-size:80%;background:gray;border:1px solid #dddddd;">';
            
            var res = responseData.find(".search_result").parent().html();
            detail = detail + (res == null ? " " : res);
            
            detail = detail + "</table>";
            
            var previewData = {
              query: searchText,
              detail: detail
            };
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
              jQuery("#yuupack", feed.dom).html(),
              previewData);
          }
        });

    },

    execute: function( directObject ) {
            Utils.openUrlInBrowser( 'http://tracking.post.japanpost.jp/service/singleSearch.do?searchKind=S002&locale=ja&reqCodeNo1=' + directObject.text );
    }
});

CmdUtils.CreateCommand({
    name: "kakitome",
    description: "書留記録問い合わせ",
    takes: {search: noun_arb_text},
    icon: "http://www.japanpost.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 11) {
            var previewStr = "一般書留、現金書留、簡易書留、特定記録郵便問い合わせ";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> の書留記録を問い合わせます...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: 'http://tracking.post.japanpost.jp/service/singleSearch.do?searchKind=S001&locale=ja&reqCodeNo1=' + searchText,
          dataType: "html",
          
          error: function() {
            previewBlock.innerHTML = "Error asking Kakitome.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            
            responseData.find("A").each(function()jQuery(this).replaceWith("<div>" + jQuery(this).text() + "</div>"));
            
            var detail = "";
            
            detail = responseData.find(".message_font").text();
            detail += responseData.find(".msg_warning").text();
            
            detail += '<style>th,td{width:auto;height:auto;border:1px solid darkslategray;}</style><table style="width:80%;font-size:80%;background:gray;border:1px solid #dddddd;">';
            
            var res = responseData.find(".search_result").parent().html();
            detail += (res == null ? " " : res);
            
            detail += "</table>";
            
            var previewData = {
              query: searchText,
              detail: detail
            };
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
              jQuery("#kakitome", feed.dom).html(),
              previewData);
          }
        });

    },

    execute: function( directObject ) {
            Utils.openUrlInBrowser( 'http://tracking.post.japanpost.jp/service/singleSearch.do?searchKind=S001&locale=ja&reqCodeNo1=' + directObject.text );
    }
});


Number.prototype.fillZero = function(n) {
    return Array((n+1) - this.toString().split('').length).join('0') + this;
}


]]></script>
</body>
</html>
